#include <stdio.h>
#include <stdlib.h>
#include "Includes/Color.h"
#include "Includes/Matrix.h"
#include "Includes/Defines.h"
#include "Includes/Rectangle.h"
#include "Includes/Surface.h"
#include "Includes/Context.h"
#include "Includes/Driver.h"
#include "Includes/Display.h"
#include <unistd.h>

/// <summary>
/// Locals
/// </summary>
static int display_count = 0;
static Display displays[GAL_MAX_DISPLAYS];

/// <summary>
/// TODO
/// </summary>
int GalDisplayInit(DriverPtr drv)
{
	if (drv == NULL)
		return -1;

	if (display_count >= GAL_MAX_DISPLAYS)
		return -2;

	displays[display_count].x = 0;
	displays[display_count].y = 0;
	displays[display_count].width = 100;
	displays[display_count].height = 100;
	displays[display_count].state = 1;
	displays[display_count].drv = drv;
	display_count++;

	return 0;
}

/// <summary>
/// TODO
/// </summary>
int GalDisplaySetGamma(void)
{
	return 0;
}

/// <summary>
/// TODO
/// </summary>
int GalDisplayIsActive()
{
	return 0;
}

/// <summary>
/// TODO
/// </summary>
int GalDisplayIsMain(int displayID)
{
	if (displayID >= display_count)
		return -1;

	return 0;
}

/// <summary>
/// TODO
/// </summary>
int GalDisplayCount()
{
	return display_count;
}

/// <summary>
/// Returns a graphics
/// context suitable for drawing
/// to a captured display.
/// </summary>
ContextPtr GalDisplayGetDrawingContext(int displayID)
{
	ContextPtr ctx;
	if (displayID >= display_count)
		return NULL;
	//ctx = &displays[displayID].ctx;
	return ctx;
}

/// <summary>
/// Returns a Boolean value
/// indicating whether a display
/// is connected or online.
/// </summary>
int GalDisplayIsOnline(int displayID)
{
	if (displayID >= display_count)
		return -1;
	if (displays[displayID].connection <= 0)
		return -1;
	return 0;
}

/// <summary>
/// Returns a Boolean value
/// indicating whether a display
/// is connected or online.
/// </summary>
int GalDisplaySwap(int displayID, RectanglePtr clip)
{
	DisplayPtr dsp;

	if (displayID >= display_count)
		return -1;

	if (!(dsp = &displays[displayID]))
		return -2;

	if (dsp->back == NULL)
		return -3;

	if (dsp->front == NULL)
		return -3;

	if (dsp->drv == NULL)
		return -4;

	return 0;
}

/// <summary>
/// TODO
/// </summary>
int GalDisplaySetCursorPosition(int x, int y)
{
	return 0;
}

/// <summary>
/// TODO
/// </summary>
int GalDisplayExit(void)
{
	return 0;
}
